{block name=body}
{* Define the function *} {function name=options_list level=0} 
	{foreach $opts as $opt}
		{if $fields.listing_id neq $opt.id}
			<option value="{$opt.id}" {if  $selected eq $opt.id}selected="selected"{/if}>{for $var=1 to $level}- {/for}{$opt.value}</option>
			{if count($opt.subs) > 0} {call name=options_list opts=$opt.subs level=$level+1 selected=$selected} {/if} 
		{/if}
	{/foreach} 
{/function}

<div class="row">
	<div class="col-sm-12">
		<form class="well form-horizontal" id="Edit_Record" accept-charset="UTF-8" method="post">
			<div class="row">
				<div class="col-sm-12 edit-page-header">
							<span class="edit-page-title">{if $fields.listing_id neq ""}编辑{else}新建{/if} {$zone}</span>
							{if $cnt eq ""}{assign var=cnt value=0}{/if}
							<a href="javascript:void(0);" onClick="$('#Edit_Record').submit();" class="btn btn-primary pull-right top-btn"><span class="glyphicon glyphicon-floppy-saved"></span> 保存</a> 
							<div class="published" {if $fields.listing_published eq 0}style="display:none;"{/if}>
								<!-- 已发布 -->
								<a href="javascript:void(0);" onClick="unpublish('listing_published');" class="btn btn-warning pull-right top-btn"><span class="glyphicon glyphicon-thumbs-down"></span> 不发布</a>
							</div>
							<div class="drafts" {if $fields.listing_published eq 1}style="display:none;"{/if}>
								<!-- 草稿 -->
								<a href="javascript:void(0);" onClick="publish('field[1][tbl_listing][{$cnt}][id]','listing_object_id','listing_published','field[1][tbl_listing][{$cnt}][listing_deleted]');" class="btn btn-primary pull-right top-btn drafts"><span class="glyphicon glyphicon-thumbs-up"></span> 保存 &amp; 发布</a>
							</div>
					<input type="hidden" value="listing_id" name="primary_id" id="primary_id"/>
					<input type="hidden" value="listing_id" name="field[1][tbl_listing][{$cnt}][id]" id="id"/>
					<input type="hidden" value="{$fields.listing_id}" name="field[1][tbl_listing][{$cnt}][listing_id]" id="listing_id" class="key">
					<input type="hidden" value="{$typeID}" name="field[1][tbl_listing][{$cnt}][listing_type_id]" id="listing_type_id">
					<input type="hidden" value="0" name="field[1][tbl_listing][{$cnt}][listing_parent_flag]" id="listing_parent_flag">
					<input type="hidden" value="0" name="field[1][tbl_listing][{$cnt}][listing_parent_id]" id="listing_parent_id">
					<input type="hidden" value="0" name="field[1][tbl_listing][{$cnt}][listing_display_menu]" id="listing_display_menu">
					<input type="hidden" value="{if $fields.listing_object_id}{$fields.listing_object_id}{else}{$objID}{/if}" name="field[1][tbl_listing][{$cnt}][listing_object_id]" id="listing_object_id">
					<input type="hidden" value="{$fields.listing_published}" name="field[1][tbl_listing][{$cnt}][listing_published]" id="listing_published">
					<input type="hidden" value="{if $fields.listing_created}{$fields.listing_created}{else}{'Y-m-d H:i:s'|date}{/if}" name="field[1][tbl_listing][{$cnt}][listing_created]" id="listing_created">
					<input type="hidden" name="formToken" id="formToken" value="{$token}" />
				</div>
			</div>
			<div class="row published" {if $fields.listing_published eq 0}style="display:none;"{/if}>
				<div class="alert alert-success text-center">
					<strong>已发布</strong> 
				</div>
			</div>
			<div class="row drafts" {if $fields.listing_published eq 1}style="display:none;"{/if}>
				<div class="alert alert-info text-center">
					<strong>草稿</strong>
				</div>
			</div>
			<ul class="nav nav-tabs" id="myTab">
				<li class="active"><a href="#details" data-toggle="tab">详情</a></li>
				<li><a href="#log" data-toggle="tab">日志</a></li>
			</ul>
		
			<div class="tab-content">
				<!--===+++===+++===+++===+++===+++ DETAILS TAB +++===+++===+++===+++===+++====-->
				<div class="tab-pane active" id="details">
					<div class="row form" data-error="<b>详情</b>选项上发现错误。打开<b>详情</b>选项以查看特定错误通知。">
						<div class="row form-group">
							<label class="col-sm-3 control-label">主体标题 *</label>
							<div class="col-sm-5">
								<select class="form-control" name="" id="newSelectValue">
									{foreach $res as $tab}
										<option  value="{$tab.id}">{$tab.title}</option>
									{/foreach}
								</select>
							</div>
							<div class="col-sm-2" id="newTitle">
								<a href="/admin/general/new/" >添加主题标题</a>
							</div>
						</div>
						<div class="row form-group">
							<label class="col-sm-3 control-label" for="id_listing_name">标题 *</label>
							<div class="col-sm-5">
								<input class="form-control" type="text" value="{$fields.listing_name}" name="field[1][tbl_listing][{$cnt}][listing_name]" id="id_listing_name" onchange="seturl(this.value);" required>
								<span class="help-block"></span>
							</div>
						</div>
						
						<input class="form-control" type="hidden" value="{$fields.listing_url}" name="field[1][tbl_listing][{$cnt}][listing_url]" id="id_listing_url" onchange="seturl(this.value, true);" >
           
           <div class="row form-group">
							<label class="col-sm-3 control-label" for="id_listing_meta_description">摘要</label>
							<div class="col-sm-5">
								<input class="form-control" type="text" value="{$fields.listing_meta_description}" name="field[1][tbl_listing][{$cnt}][listing_meta_description]" id="id_listing_meta_description">
							</div>
						</div>
						
           <div class="row form-group">
							<label class="col-sm-3 control-label" for="id_listing_content1">新闻内容</label>
							<div class="col-sm-5">
								<textarea name="field[1][tbl_listing][{$cnt}][listing_content1]" id="id_listing_content1" class="tinymce">{$fields.listing_content1}</textarea>
							</div>
						</div>
						<div class="row form-group">
							<label class="col-sm-3 control-label" for="id_news_start_date">开始日期 *</label>
							<div class="col-sm-5">
								<input type="hidden" value="news_id" name="field[2][tbl_news][{$cnt}][id]" id="id_news" />
								<input type="hidden" value="listing_id" name="default[news_listing_id]" />
								<input type="hidden" value="{$fields.news_id}" name="field[2][tbl_news][{$cnt}][news_id]" class="key"> 
								<input type="hidden" value="{$fields.listing_id}" name="field[2][tbl_news][{$cnt}][news_listing_id]" id="news_listing_id" class="key"/>
							
								<input class="form-control dates" type="text" value="{if $fields.news_start_date}{$fields.news_start_date|date_format:"%Y-%m-%d"}{else}{$smarty.now|date_format:"%Y-%m-%d"}{/if}"  name="from" id="from" onchange="setDateValue('id_news_start_date',this.value);" required>
								<input type="hidden" value="{if $fields.news_start_date}{$fields.news_start_date}{else}{$smarty.now|date_format:"%Y-%m-%d"}{/if}" name="field[2][tbl_news][{$cnt}][news_start_date]" id="id_news_start_date">
							</div>
						</div>
						<div class="row form-group">
							<label class="col-sm-3 control-label" for="id_news_end_date">结束日期 </label>
							<div class="col-sm-5">
								<input class="form-control dates" type="text" value="{if $fields.news_end_date}{$fields.news_end_date|date_format:"%Y-%m-%d"}{/if}"  name="to" id="to" onchange="setDateValue('id_news_end_date',this.value);" required>
								<input type="hidden" value="{if $fields.news_end_date}{$fields.news_end_date}{/if}" name="field[2][tbl_news][{$cnt}][news_end_date]" id="id_news_end_date">
							</div>
						</div>
           <div class="row form-group">
							<label class="col-sm-3 control-label" for="listing_image">图片<br><small>尺寸: 480px宽 * 480px高</small></label>
							<div class="col-sm-9">
								<input type="hidden" value="{$fields.listing_image}" name="field[1][tbl_listing][{$cnt}][listing_image]" id="listing_image_link" class="fileinput"> 
								<span class="file-view" id="listing_image_path"> {if $fields.listing_image}<a href="{$fields.listing_image}" target="_blank" >查看</a>{else}暂无{/if} </span> 
								<a href="javascript:void(0);" class="btn btn-info marg-5r" onclick="getFileType('listing_image','','');">Select file</a> 
								<a href="javascript:void(0);" class="btn btn-info" onclick="$('#listing_image_link').val('');$('#listing_image_path').html('None');">移除 file</a>
							</div>
						</div>     
					
						{*<div class="row form-group">*}
							{*<label class="col-sm-3 control-label" for="id_listing_flag1">重要</label>*}
							{*<div class="col-sm-5">*}
								{*<input type="hidden" value="{if $fields.listing_flag1 eq 1}1{else}0{/if}" name="field[1][tbl_listing][{$cnt}][listing_flag1]" class="value">*}
								{*<input class="chckbx" type="checkbox" {if $fields.listing_flag1 eq 1} checked="checked" {/if}*}
									 {*onclick="if($(this).is(':checked')){ $(this).parent().children('.value').val('1'); $('.important_news').show('slow'); }else{ $(this).parent().children('.value').val('0'); $('.important_news').hide('slow'); }" id="id_listing_flag1">*}
							{*</div>*}
						{*</div> *}
						<div class="row form-group important_news" {if $fields.listing_flag1 neq 1}style="display:none"{/if}>
							<label class="col-sm-3 control-label" for="id_listing_flag2">Display to Administrators</label>
							<div class="col-sm-5">
								<input type="hidden" value="{if $fields.listing_flag2 eq 1}1{else}0{/if}" name="field[1][tbl_listing][{$cnt}][listing_flag2]" class="value">
								<input class="chckbx" type="checkbox" {if $fields.listing_flag2 eq 1} checked="checked" {/if}
									 onclick="if($(this).is(':checked')){ $(this).parent().children('.value').val('1') }else{ $(this).parent().children('.value').val('0') }" id="id_listing_flag2">
							</div>
						</div> 
						<div class="row form-group important_news"  {if $fields.listing_flag1 neq 1}style="display:none"{/if}>
							<label class="col-sm-3 control-label" for="id_listing_flag3">Display to Franchisee</label>
							<div class="col-sm-5">
								<input type="hidden" value="{if $fields.listing_flag3 eq 1}1{else}0{/if}" name="field[1][tbl_listing][{$cnt}][listing_flag3]" class="value">
								<input class="chckbx" type="checkbox" {if $fields.listing_flag3 eq 1} checked="checked" {/if}
									 onclick="if($(this).is(':checked')){ $(this).parent().children('.value').val('1') }else{ $(this).parent().children('.value').val('0') }" id="id_listing_flag3">
							</div>
						</div> 
						
					</div>
				</div>
				<!--===+++===+++===+++===+++===+++ LOG TAB +++===+++===+++===+++===+++====-->
				<div class="tab-pane" id="log">
					<div class="row form" id="tags-wrapper">
						<div class="col-sm-12">
							{if $fields.logs}
								<table class="table table-bordered table-striped table-hover">
									<thead>
										<tr>
											<th>事件</td>
											<th>操作</td>
											<th>用户</td>
										</tr>
									</thead>
									<tbody>
									{foreach $fields.logs as $log}
										<tr {if $log.listing_id eq $fields.listing_id}class="info"{/if}>
											<td>{$log.log_created|date_format:"%d/%b/%Y %r"}</td>
											<td>{$log.log_action}{if $log.log_action eq 'Add' || $log.log_action eq 'Delete'} draft{/if}</td>
											<td>{$log.admin_name}</td>
										</tr>
									{/foreach}
									</tbody>
								</table>
							{else}
								暂无日志。
							{/if}
						</div>
					</div>
				</div>
			</div>
			<div class="row form-group form-bottom-btns">
				<a href="javascript:void(0);" onClick="$('#Edit_Record').submit();" class="btn btn-primary pull-right top-btn"><span class="glyphicon glyphicon-floppy-saved"></span> 保存</a>
				<div class="published" {if $fields.listing_published eq 0}style="display:none;"{/if}>
					<!-- 已发布 -->
					<a href="javascript:void(0);" onClick="unpublish('listing_published');" class="btn btn-warning pull-right top-btn"><span class="glyphicon glyphicon-thumbs-down"></span> 不发布</a>
				</div>
				<div class="drafts" {if $fields.listing_published eq 1}style="display:none;"{/if}>
					<!-- 草稿 -->
					<a href="javascript:void(0);" onClick="publish('field[1][tbl_listing][{$cnt}][id]','listing_object_id','listing_published','field[1][tbl_listing][{$cnt}][listing_deleted]');" class="btn btn-primary pull-right top-btn drafts"><span class="glyphicon glyphicon-thumbs-up"></span> 保存 &amp; 发布</a>
				</div>
			</div>
		</form>
	</div>
</div>

{include file='jquery-validation.tpl'}
<script type="text/javascript">
function seturl(str){
	seturl(str,false);
}
function seturl(str,editexisting){
	$.ajax({
		type: "POST",
	    url: "/admin/includes/processes/urlencode.php",
		cache: false,
		data: "value="+encodeURIComponent(str),
		dataType: "json",
	    success: function(res, textStatus) {
	    	try{
	    		if($('#listing_id').val() == "" || editexisting == true){
	    		$('#id_listing_url').val(res.url);
	    		}
	    	}catch(err){ }
	    }
	});
}

$(document).ready(function(){
	if ($('#newSelectValue option').length == 0) {
//		$('#newTitle').show();
		$('#newSelectValue').html("<option>请现添加主体标题</option>");
	}
	$('#Edit_Record').validate({
		onkeyup: false
	});

	$('#id_listing_url').rules("add", {
	  	  uniqueURL: {
		  		id: $('#listing_object_id').val(),
        	  	idfield: "listing_object_id",
	        	table : "tbl_listing",
	        	field : "listing_url",
	        	field2 : "listing_parent_id",
	        	value2 : "id_listing_parent"
			  }
	});

	$("#from").datepicker({
		changeMonth : true,
		changeYear : true,
		dateFormat : "dd/mm/yy",
		onSelect : function(selectedDate) {
			$("#id_news_start_date").val( convert_to_mysql_date_format(selectedDate) );
			$("#to").datepicker("option", "minDate", selectedDate);
		}
	});

	$("#to").datepicker({
		changeMonth : true,
		changeYear : true,
		dateFormat : "dd/mm/yy",
		onSelect : function(selectedDate) {
			$("#id_news_end_date").val( convert_to_mysql_date_format(selectedDate) );
			$("#from").datepicker("option", "maxDate", selectedDate);
		}
	});
});

function saveDraft(id_name,objId_name,publish_name, field_name, preview){
	if ($('#Edit_Record').valid()) {
		$('body').css('cursor', 'wait');
		$('#'+publish_name).val('0');
		var id_key0 = encodeURIComponent(id_name+'[0]');
		var id_key1 = encodeURIComponent(id_name+'[1]');
		var objId_key = encodeURIComponent($('#'+objId_name).attr('name'));
		var publish_key = encodeURIComponent($('#'+publish_name).attr('name'));
		var field_key = encodeURIComponent(field_name);
		var field_value = encodeURIComponent(mysql_now());
		$.ajax({
			type : "POST",
			url : "/admin/includes/processes/processes-record.php",
			cache: false,
			async: false,
			data : id_key0+'='+objId_name+'&'+id_key1+'='+publish_name+'&'+objId_key+"="+$('#'+objId_name).val()+"&"+publish_key+"=0&"+field_key+"="+field_value+'&formToken='+$('#formToken').val(),
			dataType: "html",
			success : function(data, textStatus) {
				try {
					var obj = $.parseJSON(data);
					if(obj.notice){ 
						$('.key').val('');
						$('#Edit_Record').submit();
						$('.published').hide();
						$('.drafts').show();
						buildUrl('tbl_listing','listing_parent_id',objId_name, preview);
					}
				} catch (err) {}
				$('body').css('cursor', 'default');
			}
		});
		$('body').css('cursor', 'default');
	}
}

function publish(id_name,objId_name,publish_name,field_name){
	if ($('#Edit_Record').valid()) {
		$('body').css('cursor', 'wait');
		$('#'+publish_name).val('1');
		var id_key0 = encodeURIComponent(id_name+'[0]');
		var id_key1 = encodeURIComponent(id_name+'[1]');
		var objId_key = encodeURIComponent($('#'+objId_name).attr('name'));
		var publish_key = encodeURIComponent($('#'+publish_name).attr('name'));
		var field_key = encodeURIComponent(field_name);
		var field_value = encodeURIComponent(mysql_now());
		$.ajax({
			type : "POST",
			url : "/admin/includes/processes/processes-record.php",
			cache: false,
			data : id_key0+'='+objId_name+'&'+id_key1+'='+publish_name+'&'+objId_key+"="+$('#'+objId_name).val()+"&"+publish_key+"=1&"+field_key+"="+field_value+'&formToken='+$('#formToken').val(),
			dataType: "html",
			success : function(data, textStatus) {
				try {
					var obj = $.parseJSON(data);
					if(obj.notice){
						$('#Edit_Record').submit();
						$('.drafts').hide();
						$('.published').show();
					}
				} catch (err) {}
				$('body').css('cursor', 'default');
			}
		});
		$('body').css('cursor', 'default');
	}
}

function unpublish(publish_name){
	$('#'+publish_name).val('0');
	$('#Edit_Record').submit();
	$('.published').hide();
	$('.drafts').show();
}

function newTag() {
	if ( $('#new_tag').val() != '' ) { 
		$('body').css('cursor', 'wait');
		var no = $('#tagno').val();
		no++;
		$('#tagno').val(no);
		$.ajax({
			type : "POST",
			url : "/admin/includes/processes/processes-tags.php",
			cache : false,
			data : "template=tag.tpl&tagno=" + no + "&tag%5Btag_value%5D="	+  $('#new_tag').val() + "&table_name=tbl_listing&default=listing_id&objId=" + $('#listing_id').val(),
			dataType : "html",
			success : function(data, textStatus) {
				try {
					$('#tags-wrapper').prepend(data);
					$('#new_tag').val('');
					$('body').css('cursor', 'default');
					$('#new_tag').closest('.form-group').removeClass('has-success').removeClass('has-error');
				} catch (err) {
					$('body').css('cursor', 'default');
				}
			}
		});
	} else {
		$('#new_tag').closest('.form-group').removeClass('has-success').addClass('has-error');
	}
}

function deleteTag(ID) {
	if (ConfirmDelete()) {
		var count = $('#' + ID).attr('rel');
		var today = new Date();
		var dd = today.getDate();
		var mm = today.getMonth() + 1;//January is 0!
		var yyyy = today.getFullYear();
		var hh = today.getHours();
		var MM = today.getMinutes();
		var ss = today.getSeconds();

		html = '<input type="hidden" value="'+yyyy+'-'+mm+'-'+dd+' '+hh+':'+MM+':'+ss+'" name="field[15][tbl_tag]['+count+'][tag_deleted]" />';
		$('#' + ID).append(html);
		$('#' + ID).css('display', 'none');
		$('#' + ID).removeClass('tags');
	} else {
		return false;
	}
}

</script>
{/block}
